package com.css.zfzx.data.config;


import com.alibaba.druid.pool.DruidDataSource;
import org.hibernate.cfg.Environment;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;


@Configuration
@EnableJpaRepositories(
        basePackages = "com.css.zfzx.data.thirdary.*.repository",
        entityManagerFactoryRef = "thirdaryEntityManager",
        transactionManagerRef = "thirdaryTransactionManager"
)
public class ThirdaryConfig {

    @Value("${spring.datasource.thirdary.jdbc-url}")
    private String url;
    @Value("${spring.datasource.thirdary.password}")
    private String password;
    @Value("${spring.datasource.thirdary.username}")
    private String username;

    @Bean
    public LocalContainerEntityManagerFactoryBean thirdaryEntityManager() {
        LocalContainerEntityManagerFactoryBean em
                = new LocalContainerEntityManagerFactoryBean();
        em.setPackagesToScan("com.css.zfzx.data.thirdary.*.*.entity");
        em.setDataSource(thirdaryDataSource());
        HibernateJpaVendorAdapter vendorAdapter
                = new HibernateJpaVendorAdapter();
        vendorAdapter.setShowSql(false);
        vendorAdapter.setDatabase(Database.MYSQL);
        vendorAdapter.setGenerateDdl(true);
        em.setJpaVendorAdapter(vendorAdapter);
        HashMap<String, Object> properties = new HashMap<>(2);
        properties.put(Environment.PHYSICAL_NAMING_STRATEGY, new SpringPhysicalNamingStrategy());
        properties.put(Environment.AUTOCOMMIT, true);

//        properties.put("hibernate.naming.physical-strategy", "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy");
//        properties.put("hibernate.naming.implicit-strategy", "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy");
        em.setJpaPropertyMap(properties);
        return em;
    }

    @Bean
    public DataSource thirdaryDataSource() {
//        DriverManagerDataSource dataSource
//                = new DriverManagerDataSource();
//        BasicDataSource dataSource = new BasicDataSource();
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setValidationQuery("select 1");
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl(url);
        dataSource.setPassword(password);
        dataSource.setUsername(username);
        return dataSource;
    }

    @Bean
    public PlatformTransactionManager thirdaryTransactionManager() {
        JpaTransactionManager transactionManager
                = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(
                thirdaryEntityManager().getObject());
        return transactionManager;
    }

}
